package com.walterlife.javacore;

import java.util.logging.Level;
import java.util.logging.Logger;

public class JoinTest {
    public static void main (String [] args) {
        Thread ta = new Thread(new ThreadA(), "ThreadA");
        Thread tb = new Thread(new ThreadB(), "ThreadB");
        Thread tc = new Thread(new ThreadC(), "ThreadC");
        try {
            ta.start();
            // join 方法 使得 调用的线程必须先执行完，再接着执行下面的代码
            ta.join();
            tb.start();
            tb.join();
            tc.start();
            tc.join();
            //tc.wait(); // 不在 synchronized 代码块内执行,会抛出异常
            System.out.printf("Thrad name: %s\n", Thread.currentThread().getName());
        } catch(InterruptedException ex) {
            Logger.getLogger(JoinTest.class.getName()).log(Level.SEVERE, null, ex);            
        }
    }
}

class ThreadA implements Runnable {

    public void run() {
        System.out.printf("Thread name: %s\n", Thread.currentThread().getName());
    } 
}    

class ThreadB implements Runnable {
    public void run() {
        System.out.printf("Thread name: %s\n", Thread.currentThread().getName());
    } 
} 

class ThreadC implements Runnable {
    public void run() {
        System.out.printf("Thread name: %s\n", Thread.currentThread().getName());
    } 
} 
